﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cs499.Database
{
    public partial class Course
    {
        public bool Delete()
        {
            bool retval = true;
            Model1Container db = new Model1Container();
            Course course = db.Set<Course>()
                .Include("PlaceRequirements")
                .Include("PrereqCourses")
                .Include("CourseInstances")
                .FirstOrDefault(coursetofind => coursetofind.Number == this.Number && coursetofind.Id == this.Id);

            if(course == null)
            {
                return false;
            }


            foreach (Place pc in course.PlaceRequirements)
            {
                retval = retval && pc.DependentCourses.Remove(course);
            }
            foreach (CourseInstance ci in course.CourseInstances.ToList())
            {
                
                db.Set<CourseInstance>().Remove(ci);
            }
            try
            {
                db.Set<Course>().Remove(course);
                db.SaveChanges();
            }
            catch (Exception /**/)
            {
                return false;
            }
            return retval;
        }
        public override string ToString()
        {
            return this.Number.ToString();
        }
    }
}